package org.javaboy.idemponent.service;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class TokenService {
    @Autowired
    StringRedisTemplate redisTemplate;
    public String generateToken() {
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(token, token);
        return token;
    }

    public boolean checkToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (token == null) {
            return false;
        }
        //去 Redis 上删除这个令牌，看能否删除成功
        if (redisTemplate.delete(token)) {
            return true;
        }
        return false;
    }
}
